package com.acwing.partition2;

import java.io.*;
import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2022/3/3 9:33
 */
public class AC122糖果传递 {

    private static final int N = 1000010;
    private static int[] a = new int[N];
    private static long[] c = new long[N];
    private static int n;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        n = Integer.parseInt(reader.readLine());
        long sum = 0, avg = 0, ans = 0;
        for (int i = 1; i <= n; i++) {
            a[i] = Integer.parseInt(reader.readLine());
            sum += a[i];
        }
        avg = sum / n;
        for (int i = n; i > 1; i--) c[i] = c[i + 1] + avg - a[i];
        c[1] = 0;
        Arrays.sort(c, 1, n + 1);
        for (int i = 1; i <= n; i++) ans += Math.abs(c[i] - c[(n + 1) / 2]);
        writer.write(ans + "\n");
        writer.flush();
    }
}
